package exception;

import java.util.Arrays;
import java.util.List;

/**
 * @Author Justin Parker
 * @Date 2024/7/18 0018 15:03
 * @Version 1.0
 */
public class StreamDemo02 {
    public static void main(String[] args) {

        List<Integer> list1 = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
        //1.求集合中奇数的个数
        long count = list1.stream()
                            .filter(x ->x%2!=0)
                            .count();
        System.out.println(count);
        //2.求集合中所有偶数的和
        //方案一：-sum()
        int sum = list1.stream()
                .filter(x ->x%2==0)
                .mapToInt(x->x)
                .sum();

        System.out.println(sum);
        //方案二：-reduce()
        int sum1 = list1.stream()
                .filter(x ->x%2==0)
                .reduce(0,(x,y)->x+y);
        System.out.println(sum);
    }
}
